package com.hp.util;

import java.io.PrintWriter;
import java.util.List;
import java.util.Map;

/**
 * @author 23129
 * 实体类生成工具
 */
public class Entity {

    /**
     * 查询数据库字段名称name和备注信息comment和类型type的sql语句
     */
    static String sql = "select COLUMN_NAME name,DATA_TYPE type,COLUMN_COMMENT comment from information_schema.`COLUMNS` where TABLE_SCHEMA=? and TABLE_NAME=?";
    /**
     * 数据库名称和表名称
     */
    static String[] table = {"hisystem", "user_role"};

    /**
     * 包名
     */
    static String packages = "com.hp";
    /**
     * 表名
     */
    static String tableName = Change_Upper(table[1]);
    /**
     * 获取的所有参数
     */
    static StringBuffer parameter = new StringBuffer();

    /**
     * service生成
     *
     * @param args
     */
    public static void main1(String[] args) {
        /**
         *service类头部拼接 命名clz与outPW 传值一致 clz \n换行
         */
    }

    /**
     * 字符串拼接生成实体类
     * private Integer rid
     */
    public static void main(String[] args) {
        /**
         *实体类头部拼接 命名clz与outPW 传值一致 clz \n换行
         */
        String clz = "package ${packages}.bean;\n" +
                "\n" +
                "import lombok.Data;\n" +
                "import java.util.Date;\n" +
                "\n" +
                "@Data\n" +
                "public class ${tableName} {\n" +
                "     ${parameter}\n" +
                "}";
        DBHelper dbHelper = DBHelper.getMyHepler();
        List<Map> list = dbHelper.select(sql, table);
        /**
         * convertDataType 类型转换
         */
        for (Map map : list) {
            parameter.append("    /**\n")
                    .append("    *" + map.get("comment") + "\n")
                    .append("    **/\n")
                    .append("private " + convertDataType(map.get("type").toString()) + " " + map.get("name") + ";\n");
        }
        System.out.println(clz.replace("${packages}", packages)
                .replace("${tableName}", tableName)
                .replace("${parameter}", parameter));
        /**
         * outPW将更改后的文件输出 replace 替换
         */
        outPW(clz.replace("${packages}", packages)
                .replace("${tableName}", tableName)
                .replace("${parameter}", parameter.toString()), tableName.concat(".java"));
    }

    /**
     * 将更改后的文件输出
     *
     * @param clz
     * @param path
     */
    public static void outPW(String clz, String path) {
        PrintWriter pw = null;
        try {
            pw = new PrintWriter(path, "UTF-8");
            pw.write(clz);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (pw != null) {
                pw.close();
            }
        }
    }

    /**
     * 首字母大写，如果有_,_移除并且之后的第一个字母大写
     *
     * @param s1
     * @return
     */
    public static String Change_Upper(String s1) {
        String r = "";
        /**
         * 以_为分隔符，将单词分开
         */
        String[] a = s1.split("_");
        String[] b = new String[a.length + 1];
        for (int i = 0; i < a.length; i++) {
            //substring(0,1)首字母，toUpperCase()大写
            b[i] = a[i].substring(0, 1).toUpperCase() + a[i].substring(1, a[i].length());
            r = r + b[i];
        }
        return r;
    }

    //类型转换
    public static String convertDataType(String dataType) {
        switch (dataType) {
            case "varchar":
            case "longtext":
            case "text":
            case "char":
                return "String";
            case "double":
                return "Double";
            case "int":
            case "tinyint":
                return "Integer";
            case "bigint":
                return "Long";
            case "datetime":
            case "timestamp":
            case "Date":
                return "Date";
            case "decimal":
                return "BigDecimal";
            default:
                return "";
        }
    }
}


